import { transformSpecialTokens, transform } from './util'
let currentEditedField = null

export default {
  methods: {
    // 类型检查
    checkType(value) {
      let _toString = Object.prototype.toString
      return _toString.call(value).slice(8, -1)
    },
    openEdit() {
      if (currentEditedField && currentEditedField !== this) {
        currentEditedField.cancelEdit()
      }
      this.editedValue = transformSpecialTokens(
        JSON.stringify(transform(this.dataItem)),
        true
      )
      // this.editedKey = this.field.key
      this.editing = true
      currentEditedField = this
      this.$nextTick(() => {
        const el = this.$refs['editInput']
        el.focus()
        el.setSelectionRange(0, el.value.length)
      })
    },
    cancelEdit() {
      this.editing = false
      currentEditedField = null
    }
  }
}
